package service

import (
    "gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/repository"
	"gitlab.yoyiit.com/youyi/go-core/store"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"golang.org/x/net/context"
	"encoding/json"
)

type ProductCustomFieldListRowService interface {
	ListProductCustomFieldListRow(ctx context.Context, req *api.ListProductCustomFieldListRowRequest) (*api.ListProductCustomFieldListRowResponse, error)
	GetProductCustomFieldListRow(ctx context.Context, req *api.ProductCustomFieldListRowData) (*api.ProductCustomFieldListRowData, error)
	AddProductCustomFieldListRow(ctx context.Context, req *api.ProductCustomFieldListRowData) error
	EditProductCustomFieldListRow(ctx context.Context, req *api.ProductCustomFieldListRowData) error
	DeleteProductCustomFieldListRow(ctx context.Context, id int64) (err error)
	BatchDeleteProductCustomFieldListRow(ctx context.Context, req *api.BatchDeleteProductCustomFieldListRowRequest) (err error)
}

type somsService struct {
	productCustomFieldListRowRepo repo.ProductCustomFieldListRowRepo
}

func (s *somsService) ListProductCustomFieldListRow(ctx context.Context, req *api.ListProductCustomFieldListRowRequest) (*api.ListProductCustomFieldListRowResponse, error) {
	dbDatas, count, err := s.productCustomFieldListRowRepo.List(ctx, "", req.PageNum, req.PageSize, &repo.ProductCustomFieldListRowDBDataParam{
	    ProductCustomFieldListRowDBData: repo.ProductCustomFieldListRowDBData {
            ColumnId: req.Data.ColumnId,
			JsonValue: req.Data.JsonValue,
			
	    },
	})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	var result []*api.ProductCustomFieldListRowData
	if dbDatas != nil {
		result = make([]*api.ProductCustomFieldListRowData, len(*dbDatas))
		for i, data := range *dbDatas {
			result[i] = &api.ProductCustomFieldListRowData{
				Id:                  data.Id,
                OrganizationId:      data.OrganizationId,
                CreatedAt:           util.FormatDateTime(data.CreatedAt),
                UpdatedAt:           util.FormatDateTime(data.UpdatedAt),
                CreatedUserId:       data.CreatedUserId,
                UpdatedUserId:       data.UpdatedUserId,
                UpdatedUserNickname: data.UpdatedUserNickname,
                CreatedUserNickname: data.CreatedUserNickname,
				ColumnId: data.ColumnId,
	JsonValue: data.JsonValue,
	
			}
		}
	}
	return &api.ListProductCustomFieldListRowResponse{
		Data:  result,
		Count: count,
	}, nil
}

func (s *somsService) GetProductCustomFieldListRow(ctx context.Context, req *api.ProductCustomFieldListRowData) (*api.ProductCustomFieldListRowData, error) {
	data, err := s.productCustomFieldListRowRepo.Get(ctx, &repo.ProductCustomFieldListRowDBData{
		BaseDBData: repository.BaseDBData{
        			BaseCommonDBData: repository.BaseCommonDBData{
        				Id: req.Id,
        			},
        		},
		})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	resp := &api.ProductCustomFieldListRowData{
		Id:                  data.Id,
        OrganizationId:      data.OrganizationId,
        CreatedAt:           util.FormatDateTime(data.CreatedAt),
        UpdatedAt:           util.FormatDateTime(data.UpdatedAt),
        CreatedUserId:       data.CreatedUserId,
        UpdatedUserId:       data.UpdatedUserId,
        UpdatedUserNickname: data.UpdatedUserNickname,
        CreatedUserNickname: data.CreatedUserNickname,
		ColumnId: data.ColumnId,
	JsonValue: data.JsonValue,
	
	}
	return resp, nil
}

func (s *somsService) AddProductCustomFieldListRow(ctx context.Context, data *api.ProductCustomFieldListRowData) error {
	repoData := &repo.ProductCustomFieldListRowDBData{
		ColumnId: data.ColumnId,
	JsonValue: data.JsonValue,
	
	}
	_, err := s.productCustomFieldListRowRepo.Add(ctx, repoData)
	if err != nil {
		return handler.HandleError(err)
	}
	return nil
}

func (s *somsService) EditProductCustomFieldListRow(ctx context.Context, data *api.ProductCustomFieldListRowData) error {
	repoData := &repo.ProductCustomFieldListRowDBData{
		BaseDBData: repository.BaseDBData{
        			BaseCommonDBData: repository.BaseCommonDBData{
        				Id: data.Id,
        			},
        		},
		ColumnId: data.ColumnId,
	JsonValue: data.JsonValue,
	
	}
	return handler.HandleError(s.productCustomFieldListRowRepo.UpdateById(ctx, data.Id, repoData))
}

func (s *somsService) DeleteProductCustomFieldListRow(ctx context.Context, id int64) (err error) {
	return handler.HandleError(s.productCustomFieldListRowRepo.DeleteById(ctx, id))
}

func (s *somsService) BatchDeleteProductCustomFieldListRow(ctx context.Context, req *api.BatchDeleteProductCustomFieldListRowRequest) (err error) {
	if req.Ids != nil && len(req.Ids) > 0 {
		// 自行补充逻辑
	}
	return nil
}
